/**
 * 
 */
package com.williamoneil.charts.client.widget;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.williamoneil.charts.client.data.ChartBoundsRect;
import com.williamoneil.charts.client.data.ChartElementsData;

/**
 * @author gudipati
 *
 */
public abstract class ChartOverlay {
 
	private int zIndex = 0;
	
	private ChartBoundsRect chartBoundsRect = null;
	private ChartElementsData chartElements = null;
	
	private Canvas canvas = null;

	public ChartOverlay(){
		canvas = Canvas.createIfSupported();
	}
	
	public ChartOverlay(ChartBoundsRect bounds) {
		this();
		this.chartBoundsRect = bounds;
		
		canvas.setSize("100%", "100%");
		canvas.setCoordinateSpaceHeight(bounds.getTotalHeight());
		canvas.setCoordinateSpaceWidth(bounds.getTotalWidth());
	}
	
	/**
	 * @return the zIndex
	 */
	public int getzIndex() {
		return zIndex;
	}

	/**
	 * @param zIndex the zIndex to set
	 */
	public void setzIndex(int zIndex) {
		this.zIndex = zIndex;
	}

	/**
	 * @return the canvas
	 */
	public Canvas getCanvas() {
		return canvas;
	}

	/**
	 * @param canvas the canvas to set
	 */
	public void setCanvas(Canvas canvas) {
		this.canvas = canvas;
	}
	
	public Context2d getGraphicsContext() {
		return canvas == null ? null : canvas.getContext2d();
	}
	
	/**
	 * @return the chartBoundsRect
	 */
	public ChartBoundsRect getChartBoundsRect() {
		return chartBoundsRect;
	}
	
	/**
	 * @return the chartElements
	 */
	public ChartElementsData getChartElements() {
		return chartElements;
	}

	/**
	 * @param chartBounds the chartBounds to set
	 */
	public void resetChartBounds(ChartBoundsRect newChartBounds) {		
		
		final ChartBoundsRect oldChartBounds = this.chartBoundsRect;
		this.chartBoundsRect = newChartBounds;

		if(oldChartBounds != null) {
			clearCanvas();
		}

		canvas.setCoordinateSpaceHeight(chartBoundsRect.getTotalHeight());
		canvas.setCoordinateSpaceWidth(chartBoundsRect.getTotalWidth());

		this.drawOverlay();
	}

	public void clearCanvas() {
		// clear old drawing
		this.getGraphicsContext().clearRect(0, 0, getChartBoundsRect().getTotalWidth(), getChartBoundsRect().getTotalHeight());
	}
	
	public void resetData(ChartElementsData chartElements) {
		this.chartElements = chartElements;
	}
	
	//public abstract void resize(ChartBoundsRect oldChartBounds);
	public abstract void drawOverlay();
}
